package com.common.account.config;


import com.common.account.config.service.UserDetailsServiceImpl;
import com.common.account.endpoint.Logout;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 认证服务配置
 *
 * @author wangguochao
 */
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        // 设置默认的加密方式
        return new BCryptPasswordEncoder();
    }

    /**
     * 注入用户信息服务
     *
     * @return 用户信息服务对象
     */
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }

    /**
     * 退出
     */
    @Bean
    public Logout logout() {
        return new Logout();
    }

    /**
     * 全局用户信息
     *
     * @param auth 认证管理
     * @throws Exception 用户认证异常信息
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        // 使用自定义认证与授权
        auth.authenticationProvider(new LoginAuthenticationProvider(userDetailsService()));
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login/**", "/login.html").permitAll()
                .anyRequest().authenticated()
                .and()
                .logout()
                .logoutUrl("/oauth/logout")
                .deleteCookies("JSESSIONID")
                .logoutSuccessHandler(logout())
                .logoutSuccessUrl("/oauth/authorize?client_id=web-pc&response_type=code")
                .and()
                .formLogin()
                .loginProcessingUrl("/login")
                //指定登录页的路径
                .loginPage("/login")
                //必须允许所有用户访问我们的登录页（例如未验证的用户，否则验证流程就会进入死循环）
                .permitAll();
        //默认都会产生一个hiden标签 里面有安全相关的验证 防止请求伪造 这边我们暂时不需要 可禁用掉
        http.csrf().disable();

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/css/**", "/js/**",
                        "/fonts/**", "/static/**", "/oauth/check_token", "/code/**");
    }

    /**
     * 认证管理
     *
     * @return 认证管理对象
     * @throws Exception 认证异常信息
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}
